package com.wuye.entity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import lombok.Getter;
import lombok.Setter;

import com.wuye.common.util.SpringUtil;
import com.wuye.common.util.string.StrUtil;
import com.wuye.constants.BaseConstants;
import com.wuye.dao.UserDao;

/**
 * User entity. @author MyEclipse Persistence Tools
 */

public class User extends BaseEntity implements java.io.Serializable {

	// Fields

	private Integer userId;
	private String account;
	private String pwd;
	private String userType;
	private Set userAuths = new HashSet(0);
	private Set complaintDeals = new HashSet(0);
	private Set userOrgRels = new HashSet(0);
	@Setter
	@Getter
	private PropertyCompany ownerCompany;
	private Organization ownerOrg;
	private Community ownerCommunity;
	private PartyInfo partyInfo;
	@Getter
	@Setter
	private String opendId;
	@Getter
	@Setter
	private String opendTime;
	/**
	 * 用户拥有的角色
	 */
	private List<Role> roles;
	/**
	 * 用户拥有的权限：包括用户的角色包含的权限
	 */
	private List<Privilege> privileges;
	
	/**
	 * 不包括用户的角色包含的权限
	 */
	private List<Privilege> directPrivileges;
	
	/**
	 * 用户关联的单位
	 */
	private List<Organization> orgs ;
	// Constructors
	/** default constructor */
	public User() {
	}

	/** minimal constructor */
	public User(String account, String pwd, String userType) {
		this.account = account;
		this.pwd = pwd;
		this.userType = userType;
	}

	/** full constructor */
	public User(String account, String pwd, String userType, String statusCd,
			Timestamp createDate, Timestamp statusDate, Timestamp updateDate,
			Set userAuths, Set complaintDeals, Set userOrgRels) {
		this.account = account;
		this.pwd = pwd;
		this.userType = userType;
		this.statusCd = statusCd;
		this.createDate = createDate;
		this.statusDate = statusDate;
		this.updateDate = updateDate;
		this.userAuths = userAuths;
		this.complaintDeals = complaintDeals;
		this.userOrgRels = userOrgRels;
	}

	// Property accessors

	public Integer getUserId() {
		return super.getId();
	}

	public void setUserId(Integer userId) {
		super.setId(userId);
	}

	public String getAccount() {
		return this.account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPwd() {
		return this.pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getUserType() {
		return this.userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public Set getUserAuths() {
		return this.userAuths;
	}

	public void setUserAuths(Set userAuths) {
		this.userAuths = userAuths;
	}

	public Set getComplaintDeals() {
		return this.complaintDeals;
	}

	public void setComplaintDeals(Set complaintDeals) {
		this.complaintDeals = complaintDeals;
	}

	public Set getUserOrgRels() {
		return this.userOrgRels;
	}

	public void setUserOrgRels(Set userOrgRels) {
		this.userOrgRels = userOrgRels;
	}
	public Organization getOwnerOrg(){
		if (this.isLoaded("ownerOrg", this.ownerOrg)){
			return this.ownerOrg;
		}
		if (this.getUserOrgRels() != null && this.getUserOrgRels().size() > 0){
			List<UserOrgRel> userOrgs = new ArrayList<UserOrgRel>(this.getUserOrgRels());
			if (userOrgs.get(0).getOrganization() == null){
				this.ownerOrg = null;
				this.Loaded("ownerOrg");
				return this.ownerOrg;
			}else{
				Organization org = userOrgs.get(0).getOrganization();
				this.ownerOrg = org;
				this.Loaded("ownerOrg");
				return this.ownerOrg;
			}
		}else{
			this.ownerOrg = null;
			this.Loaded("ownerOrg");
			return this.ownerOrg;
		}
	}
	
	/**
	 * 获取用户关联的所有单位
	 * @return
	 */
	public List<Organization> getOrgs(){
		if (this.isLoaded("orgs", this.orgs)){
			return this.orgs;
		}
		if (this.getUserOrgRels() != null && this.getUserOrgRels().size() > 0){
			List<UserOrgRel> userOrgs = new ArrayList<UserOrgRel>(this.getUserOrgRels());
			if (userOrgs == null || userOrgs.size() < 1){
				this.orgs = null;
				this.Loaded("orgs");
				return this.orgs;
			}else{
				List<Organization> orgTmp = new ArrayList<Organization>();
				for (UserOrgRel tmpOrgRel : userOrgs){
					if (tmpOrgRel.getOrganization() != null){
						orgTmp.add(tmpOrgRel.getOrganization());
					}
					
				}
				this.orgs = orgTmp;
				this.Loaded("orgs");
				return this.orgs;
			}
		}else{
			this.orgs = null;
			this.Loaded("orgs");
			return this.orgs;
		}
	}
	/*
	 * 在USER表保存了所属的物业公司了
	 public PropertyCompany getOwnerCompany(){
		if (this.isLoaded("ownerCompany", this.ownerCompany)){
			return this.ownerCompany;
		}
		if (this.getUserOrgRels() != null && this.getUserOrgRels().size() > 0){
			List<UserOrgRel> userOrgs = new ArrayList<UserOrgRel>(this.getUserOrgRels());
			if (userOrgs.get(0).getOrganization() == null){
				this.ownerCompany = null;
				this.Loaded("ownerCompany");
				return this.ownerCompany;
			}else{
				Organization org = userOrgs.get(0).getOrganization();
				this.ownerCompany = org.getOwnerCompany();
				this.Loaded("ownerCompany");
				return this.ownerCompany;
			}
		}else{
			this.ownerCompany = null;
			this.Loaded("ownerCompany");
			return this.ownerCompany;
		}
		
	}*/
	/**
	 * 取用户所属小区 chaijinchun 20160117
	 * @return
	 */
	public Community getOwnerCommunity(){
		if (this.isLoaded("ownerCommunity", this.ownerCommunity)){
			return this.ownerCommunity;
		}
		if (this.getUserOrgRels() != null && this.getUserOrgRels().size() > 0){
			List<UserOrgRel> userOrgs = new ArrayList<UserOrgRel>(this.getUserOrgRels());
			if (userOrgs.get(0).getOrganization() == null){
				this.ownerCommunity = null;
				this.Loaded("ownerCommunity");
				return this.ownerCommunity;
			}else{
				Organization org = userOrgs.get(0).getOrganization();
				this.ownerCommunity = org.getOwnerCommunity();
				this.Loaded("ownerCommunity");
				return this.ownerCommunity;
			}
		}else{
			this.ownerCommunity = null;
			this.Loaded("ownerCommunity");
			return this.ownerCommunity;
		}
		
	}
	
	public PartyInfo getPartyInfo(){
		if (this.isLoaded("partyInfo", this.partyInfo)){
			return this.partyInfo;
		}
		String hql = "select a from PartyInfo a where a.classId=? and a.objId = ? and a.statusCd=? ";
		List<Object> params = new ArrayList<Object>();
		params.add(BaseConstants.CLASS_USER);
		params.add(this.getId());
		params.add(BaseConstants.STATUS_VALID);
		List<PartyInfo> rets = super.getDefaultDao().findListByHQLAndParams(hql, params);
		if (rets != null && rets.size() > 0){
			this.partyInfo = rets.get(0);
			this.Loaded("partyInfo");
			return this.partyInfo;
		}else{
			this.partyInfo = null;
			this.Loaded("partyInfo");
			return this.partyInfo;
		}
	}
	public void setPartyInfo(PartyInfo partyInfo){
		this.partyInfo = partyInfo;
	}
	
	public List<Role> getRoles(){
		if (this.isLoaded("roles", this.roles)){
			return this.roles;
		}
		if (this.userAuths != null && this.userAuths.size() > 0){
			List<Role> list = new ArrayList<Role>();
			for (Object tmpObj : userAuths){
				UserAuth auth = (UserAuth)tmpObj;
				if (auth.getRole() != null){
					list.add(auth.getRole());
				}
			}
			this.roles = list;
			this.Loaded("roles");
			return this.roles;
		}else{
			this.roles = null;
			this.Loaded("roles");
			return this.roles;
		}
	}
	
	/**
	 * 获取用户的直接授权，不包括角色授权
	 * @return
	 */
	public List<Privilege> getDirectPrivileges(){
		if (this.isLoaded("directPrivileges", this.directPrivileges)){
			return this.directPrivileges;
		}
		List<Privilege> list = this.getDao().getPrivileges(this, true);
		list = new ArrayList<Privilege>(new HashSet<Privilege>(list));
		this.directPrivileges = list;
		this.Loaded("directPrivileges");
		return this.directPrivileges;
	}
	
	/**
	 * 获取用户所有权限，包括角色授权和直接授权
	 * @return
	 */
	public List<Privilege> getPrivileges(){
		if (this.isLoaded("privileges", this.privileges)){
			return this.privileges;
		}
		List<Privilege> list = this.getDao().getPrivileges(this, false);
		list = new ArrayList<Privilege>(new HashSet<Privilege>(list));
		this.privileges = list;
		this.Loaded("privileges");
		return this.privileges;
	}

	public void save(){
		super.save();
		//用户信息保存
		if (this.getPartyInfo() != null){
			this.partyInfo.setClassId(BaseConstants.CLASS_USER);
			this.partyInfo.setObjId(this.getId());
			this.partyInfo.save();
		}
		
		//用户角色保存
		
	}
	
	/**
	 * 判断用户是否具体特定权限
	 * @param privilegeCode
	 * @return
	 */
	public boolean hasPrivilege(String privilegeCode){
		
		if (StrUtil.isNullOrEmpty(privilegeCode)){
			return false;
		}
		List<Privilege> userPris = this.getPrivileges();
		if (userPris == null || userPris.size()<1){
			return false;
		}
		boolean result = false;
		for (Privilege userPri : userPris){
			if (privilegeCode.equals(userPri.getPrivilegeCode())){
				return result = true;
			}
		}
		
		return result;
	}
	public static UserDao getDao(){
		return (UserDao)SpringUtil.getBean("userDao");
	}
}